//
//  PersonalVC.m
//  EducationLive
//
//  Created by Apple on 2017/10/18.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "PersonalVC.h"
#import "EducationLiveGloabl.h"
#import "PersonalCellNode.h"
#import "TZImagePickerController.h"
#import "NicknameVC.h"
#import "AreaVC.h"
#import "ProfileVC.h"

@interface PersonalVC ()<ASTableDelegate, ASTableDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate,TZImagePickerControllerDelegate>
// UI
@property (nonatomic, strong) ASTableNode *tableNode;
// 头像Node
@property (nonatomic, strong)ASButtonNode *imageNode;
// Data
// 标题数据
@property (nonatomic, strong) NSArray *titleDataArray;
// 头像
@property (nonatomic, strong)UIImage *photoImage;

@end

@implementation PersonalVC

#pragma mark - life cycle

- (instancetype)init
{
    if (self = [super initWithNode:[ASDisplayNode new]]) {
        [self addTableNode];
    }
    return self;
}

- (void)addTableNode
{
    _tableNode = [[ASTableNode alloc] init];
    _tableNode.backgroundColor = RGB_255(245,245,245);
    _tableNode.delegate = self;
    _tableNode.dataSource = self;
    [self.node addSubnode:_tableNode];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 加载导航栏item
    [self loadNavItem];
    // 加载表Footer
    [self configureTableView];
    // 表标题数据
    [self tableTitleData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _tableNode.frame = self.node.bounds;
}
// 加载表Footer
- (void)configureTableView
{
    _tableNode.view.tableFooterView = [[UIView alloc]init];
    _tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - load Data
- (void)loadData
{
    
}
// 表标题数据
- (void)tableTitleData
{
    _titleDataArray = @[@"头像",@"昵称",@"性别",@"地区",@"个人简介"];
}

#pragma mark - NavigationController Item Title
// 加载导航栏item
- (void)loadNavItem
{
    self.title = @"个人资料";
}

#pragma mark - Photo Picker
// 初始化TZImagePickerController进行相册选择
- (void)initTZImagePicker
{
    // MaxImagesCount最多选择几张照片
    // columnNumber最少选择几张照片
    // pushPhotoPickerVc 是否跳到二级页面
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc]initWithMaxImagesCount:1 columnNumber:0 delegate:self pushPhotoPickerVc:NO];
    imagePickerVc.allowPreview = NO;
    imagePickerVc.showSelectBtn = NO;
    imagePickerVc.allowCrop = YES;
    imagePickerVc.cropRect = CGRectMake(0, (kScreenHeight -  kScreenWidth)/2, kScreenWidth, kScreenWidth);
    imagePickerVc.naviBgColor = GreenColor;
    imagePickerVc.naviTitleFont = [UIFont fontWithName:@"Helvetica"size:18.0];
    imagePickerVc.barItemTextFont = [UIFont fontWithName:@"Helvetica"size:15.0];
    [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton) {
        [leftButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }];
    __weak typeof (self) weakSelf = self;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (photos.count > 0) {
            weakSelf.photoImage = photos[0];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.imageNode setBackgroundImage:weakSelf.photoImage forState:ASControlStateNormal];
            });
        }
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
// 初始化照相机
- (void)initImagePickerController
{
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc]init];
    imagePickerVC.delegate = self;
    imagePickerVC.allowsEditing = YES;
    // 摄像头
    imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}
// 修改头像
- (void)changeHeadPortrait
{
    UIAlertController *headPortraitAlert = [UIAlertController alertControllerWithTitle:nil message:@"选取图片" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [headPortraitAlert addAction:cancelAlert];
    UIAlertAction *photoAlert = [UIAlertAction actionWithTitle:@"相册" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        // 初始化TZImagePickerController进行相册选择
        [self initTZImagePicker];
    }];
    [headPortraitAlert addAction:photoAlert];
    
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertAction *photographAlert = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            // 初始化照相机
            [self initImagePickerController];
        }];
        [headPortraitAlert addAction:photographAlert];
    }
    
    [self presentViewController:headPortraitAlert animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //得到图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    // 存入系统相册
     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    self.photoImage = image;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.imageNode setBackgroundImage:self.photoImage forState:ASControlStateNormal];
    });
    // 模态返回
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Gender
// 修改性别
- (void)changeGender
{
    UIAlertController *genderAlert = [UIAlertController alertControllerWithTitle:nil message:@"选取性别" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [genderAlert addAction:cancelAlert];
    UIAlertAction *manAlert = [UIAlertAction actionWithTitle:@"男" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

    }];
    [genderAlert addAction:manAlert];
    
    UIAlertAction *womanAlert = [UIAlertAction actionWithTitle:@"女" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

    }];
    [genderAlert addAction:womanAlert];
    
    [self presentViewController:genderAlert animated:YES completion:nil];
}

#pragma mark - ASTableDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleDataArray.count;
}

- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __typeof (self) __weak weakSelf = self;
    ASCellNode *(^cellNodeBlock)() = ^ASCellNode *() {
        ASCellNode *cellNode = nil;
        switch (indexPath.row) {
            case 0:{
                PersonalImageCellNode *personalImageCellNode = [[PersonalImageCellNode alloc] initWithItem:nil title:weakSelf.titleDataArray[indexPath.row]];
                [personalImageCellNode setInitClickedPersonalImageCellNodeBlock:^(PersonalImageCellNode *vc, id node) {
                    weakSelf.imageNode = node;
                    //修改头像
                    [weakSelf changeHeadPortrait];
                }];
                cellNode = personalImageCellNode;
                break;
            }
            default:
            {
                cellNode = [[PersonalCellNode alloc] initWithItem:nil title:_titleDataArray[indexPath.row]];
                break;
            }
        }
        return cellNode;
    };
    return cellNodeBlock;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 1:{
            NicknameVC *nicknameVC = [[NicknameVC alloc]init];
            [self.navigationController pushViewController:nicknameVC animated:YES];
            break;
        }
        case 2:{
            // 修改性别
            [self changeGender];
            break;
        }
        case 3:{
            AreaVC *areaVC = [[AreaVC alloc]init];
            [self.navigationController pushViewController:areaVC animated:YES];
            break;
        }
        case 4:{
            ProfileVC *profileVC = [[ProfileVC alloc]init];
            [self.navigationController pushViewController:profileVC animated:YES];
            break;
        }
        default:
            break;
    }
}
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    
}

@end
